package plugin

import (
	"gitee.com/mkwchecking/k-scaffold/config"
	"gorm.io/gorm"
)

func (p *Plugins) GetGdb() (*gorm.DB, error) {
	if p._gdb != nil {
		return p._gdb, nil
	}

	log, err := p.GetLogger()
	if err != nil {
		return nil, err
	}

	dbconf := config.Get().Database.Default

	db, cancel, err := initGorm(p.Ctx(), *dbconf, log.Logger)
	if err != nil {
		cancel()
		return nil, err
	}

	p._gdb = db
	return p._gdb, nil
}
